package com.liuxl.core.common.quartz.task;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.liuxl.core.mapper.auto.TSysDictDataMapper;
import com.liuxl.core.mapper.auto.TestMapper;
import com.liuxl.core.mapper.auto.TsysUserMapper;
import com.liuxl.core.model.auto.TSysDictData;
import com.liuxl.core.model.auto.TSysDictDataExample;
import com.liuxl.core.model.auto.Test;
import com.liuxl.core.util.DateUtils;
import com.liuxl.core.util.HttpBaseHutool;
import com.liuxl.core.util.SnowflakeIdWorker;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;

/**
 *	测试类
 * @CLASSNAME   V2Task
 * @Description 定时调度具体工作类
 * @DATE 2019/9/2 0002 15:33
 */
@Component("v2Task")
public class V2Task {
	
	private static final Logger logger = LoggerFactory.getLogger(V2Task.class);
	
	
	@Autowired
	private TsysUserMapper tsysUserMapper;
	
	
	@Autowired
	private TSysDictDataMapper tSysDictDataMapper;
	
	@Autowired
	private TestMapper  testMapper;
	
	
	private String token;
	
	private List<TSysDictData> list;
	
	private String afterdate;
	
    public void setToken(String token) {
		this.token = token;
	}

    
    
    
    public JSONObject getOrganId(String siteName) {
    	JSONObject jsonObject = new JSONObject();
        if(this.list==null) {
        	this.list = getList();
        }
       
        for(TSysDictData tdd:list) {
        	if(tdd.getDictLabel().equals(siteName)) {
        		jsonObject.put("data", tdd);
        		jsonObject.put("code", "200");
        		return jsonObject;
        	}
        }
        jsonObject.put("code", 500);
        
    	return jsonObject;
    }
    
    
    
	public List<TSysDictData> getList() {
		if(list==null) {
			TSysDictDataExample testExample=new TSysDictDataExample();
		    testExample.setOrderByClause("dict_sort ASC");
		    testExample.createCriteria().andDictTypeEqualTo("duty_type");
	        list= tSysDictDataMapper.selectByExample(testExample);
		}
		return list;
	}

	public void setList(List<TSysDictData> list) {
		
		this.list = list;
	}

	/**
     * 	考勤记录 -- 计算结果
     */
    public void runTask1()
    {
//    	if(this.token==null||"".equals(token)) {
//    		this.getToken();
//    	}
//    	if("500".equals(token)) {
//    		System.out.println("token 获取失败！！");
//    	}
//    	int num_data =0;
//    	String today= DateUtil.today();  //dev evi
////    	String today = "2019-12-05";		//test
//    	HashMap<String,Object> paramMap = new HashMap<String, Object>();
//    	paramMap.put("datetime", today);
//    	List<Map<String,Object>> listmr = markRecordMapper.getDutyDataList(paramMap);
//    	for(Map<String,Object> mapmr:listmr) {
//    		DutyRecord dr = new DutyRecord();
//    		dr.setId(SnowflakeIdWorker.getUUID());
//    		dr.setDuration(mapmr.get("duration")+"");
//    		dr.setDutyDate(today);
//    		dr.setInTime(DateUtil.parse(mapmr.get("mindate")+""));
//    		dr.setOutTime(DateUtil.parse(mapmr.get("maxdate")+""));
//    		dr.setOrganId(mapmr.get("organ_id")+"");
//    		dr.setOrganName(mapmr.get("organ_name")+"");
//    		dr.setTName(mapmr.get("t_name")+"");
//    		num_data+=dutyRecordMapper.insert(dr);
//    	}
//    	System.out.println("成功添加"+num_data+"条考勤数据。");
    	
    }
    
  
    

    /**
     * 有参任务
     * 目前仅执行常见的数据类型  Integer Long  带L  string  带 ''  bool Double 带 d
     * @param a
     * @param b
     */
    public void runTask2(Integer a,Long b,String c,Boolean d,Double e)
    {
    	//List<TsysUser> list=  tsysUserMapper.selectByExample(new TsysUserExample());
    	//System.err.println("用户查询num："+list.size());
        System.out.println("正在执行定时任务，带多个参数的方法"+a+"   "+b+" "+c+"  "+d+" "+e+"执行时间:"+new Date().toLocaleString());

    }
    
   
    
    /**
     * 	获取当月的工作日数量
     */
    public void runTask4() {
    	Date date = DateUtil.date();
    	int year = DateUtil.year(date);
    	int month = DateUtil.month(date)+1;
    	int num = DateUtils.getPerMonthWorkdays(year,month);
    	Test record = new Test();
    	record.setId(SnowflakeIdWorker.getUUID());
    	record.setName(year+""+month);
    	record.setAge(num);
    	testMapper.insert(record);
    }
    
    
    /***
     * 	每25分钟执行一次，重新获取token
     */
    public void getToken()
    {
    	this.token = HttpBaseHutool.getToken(null);
    	
        System.out.println("正在获取token");
    }
    
}
